
const app = getApp()
import {
	findByMobile
} from '../../../../api/member'
import {
  save
} from '../../../../api/couponCodeGift'
import utils from "../../../../utils/util"
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    giveType:false,
    giftForm:[],
    mobile:'',
    vipName:'',
    memberData:{}
  },

  /**
   * 组件的方法列表
   */
  methods: {
    giveClick(data){
      console.log(data)
      this.data.giftForm = data
      this.data.giveType = true
      this.data.mobile = ''
      this.data.vipName = ''
      this.data.memberData = {}
      this.setData({
        giveType:this.data.giveType,
        giftForm:this.data.giftForm,
        mobile:this.data.mobile,
        vipName:this.data.vipName,
        memberData:this.data.memberData
      })
    },
    cloneGive(){
      this.data.giveType = false
      this.setData({
        giveType:this.data.giveType,
      })
    },
    findByMobileClick(){
      var _this = this
      findByMobile(
        {
          mchId:app.globalData.mchId,
          mobile:_this.data.mobile
        }
      ).then(res=>{
        console.log(res)
        _this.data.memberData = res.data.member
        if(utils.isNull(res.data.member)){
          console.log('无会员')
          wx.showToast({
            title: '暂无查到此会员',
          })
        }else{
          _this.data.vipName = res.data.member.name
        }
        _this.setData({
          vipName:_this.data.vipName
        })
      })
    },
    mobileClick(data){
      console.log(data)
      this.data.mobile = data.detail.value
    },
    saveClick(){
      console.log(this.data.giftForm)
      console.log(this.data.memberData)
      var _this = this
      if(utils.isNull(this.data.memberData)){
        wx.showToast({
          title: '请选择会员',
        })
        return;
      }
      var data = []
      this.data.giftForm.forEach(function(item){
        var form = {
          couponId:item.couponId,
          couponCodeId:item.id,
          quantity:item.quantitys,
          receiverId:_this.data.memberData.id,
          status:0
        }
        data.push(form)
      })
      save(data).then(res=>{
        console.log(data)
        wx.showToast({
          title: '赠送成功',
        })
        this.data.giveType = false
        this.setData({
          giveType:this.data.giveType
        })
        this.triggerEvent('couponSave')
      })
    }
  }
})
